package com.codingchili.logging.model;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Robin Duda
 */
public class PlayerLeaderboard {
    private static final Integer LIMIT = 50;
    private List<LeaderboardEntry> entries = new ArrayList<>();

    public void add(LeaderboardEntry entry) {
        entries.add(entry);
        entries = entries.stream()
                .sorted((a, b) -> b.getExperience() - a.getExperience())
                .distinct()
                .limit(LIMIT)
                .collect(Collectors.toList());
    }

    public PlayerLeaderboard copy() {
        var leaderboard = new PlayerLeaderboard();
        leaderboard.entries.addAll(entries);
        return leaderboard;
    }

    public List<LeaderboardEntry> getEntries() {
        return entries;
    }

    public void setEntries(List<LeaderboardEntry> entries) {
        this.entries = entries;
    }
}
